import 'dart:convert';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_easyrefresh/material_footer.dart';
import 'package:flutter_easyrefresh/material_header.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:panda/base/SignType.dart';
import 'package:panda/base/application.dart';
import 'package:panda/entity/my_exam_order_entity.dart';
import 'package:panda/entity/order_list_entity.dart';
import 'package:panda/entity/order_list_entity2.dart';
import 'package:panda/event/UserInfoEvent.dart';
import 'package:panda/event/order_refresh.dart';
import 'package:panda/goods/ConfirmOrder.dart';
import 'package:panda/home/ChildListOrder.dart';
import 'package:panda/http/Api.dart';
import 'package:panda/http/HttpUtil.dart';
import 'package:panda/main.dart';
import 'package:panda/util/AppSaveInfoUtil.dart';
import 'package:panda/util/LoadingUtil.dart';
import 'package:panda/util/SignUtil.dart';
import '../base/Constant.dart';

class TempOrderPage extends StatefulWidget {
  var type;
  TempOrderPage({Key key, @required this.type}) : super(key: key);
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return TempOrderView();
  }
}

class TempOrderView extends State<TempOrderPage>
    with AutomaticKeepAliveClientMixin, WidgetsBindingObserver {
  List<OrderListDataData2> dataList = List();
  List<OrderListDataData> ownList = new List();
  int page = 1;
  bool isSelectAll = false;
  EasyRefreshController _controller = new EasyRefreshController();
  String token = "";
  int allNum = 0;
  double allPrice = 0;
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
    getString(TOKEN).then((value) {
      token = value;
      if (token == null) {
        return;
      }
      loadList();
    });
    APPLIC.eventBus.on<UserInfoEvent>().listen((event) {
      getString(TOKEN).then((value) {
        token = value;
        page = 1;
        loadList();
      });
    });
    APPLIC.eventBus.on<OrderRefreshEvent>().listen((event) {
      if(event.index=="1"){
        getString(TOKEN).then((value) {
          token = value;
          page = 1;
          loadList();
        });
      }
    });
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    // TODO: implement didChangeAppLifecycleState
    super.didChangeAppLifecycleState(state);
    if (state == AppLifecycleState.resumed)
      getString(TOKEN).then((value) {
        token = value;
      });
  }

  loadList() async {
    Map map = new Map();
    map['token'] = token;
    map['status'] = widget.type;
    map['p'] = page;
    map['per'] = 10;
    map['sign_type'] = SignType.GET_DAITIJIAO;
    Response<String> response =
        await HttpUtil().post(Api.GET_DAITIJIAO, data: getSign(map));
    Map userMap = json.decode(response.data);
    if (userMap['code'] != 1) {
      Fluttertoast.showToast(msg: userMap['msg']);
      return;
    }
    OrderListEntity2 homeListEntity = new OrderListEntity2.fromJson(userMap);
    if (mounted)
      setState(() {
        if (page == 1) {
          isSelectAll = false;
          allNum = 0;
          dataList.clear();
          ownList.clear();
          dataList.addAll(homeListEntity.data.datas);
          ownList.addAll(homeListEntity.data.owns);
          _controller.resetLoadState();
          _controller.finishRefresh();
        } else {
          dataList.addAll(homeListEntity.data.datas);
          ownList.addAll(homeListEntity.data.owns);
          _controller.finishLoad(
              noMore: (homeListEntity.data.datas.length == 0 &&
                  homeListEntity.data.owns.length == 0));
          if ((homeListEntity.data.datas.length == 0 &&
              homeListEntity.data.owns.length == 0&&page>1)) {
            // Fluttertoast.showToast(msg: "已获取全部数据");
          }
        }
        if (page == 1 && dataList.length < 3 && ownList.length == 0) {
          page++;
          loadList();
        }
      });
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
    // TODO: implement build
    return new Container(
      child: new Container(
          color: divideColor,
          child: Column(
            children: <Widget>[
              Expanded(
                child: new EasyRefresh(
                  controller: _controller,
                  header: MaterialHeader(),
                  footer: MaterialFooter(),
                  enableControlFinishLoad: true,
                  enableControlFinishRefresh: true,
                  child: ListView.separated(
                      itemBuilder: _renderRow,
                      separatorBuilder: (BuildContext context, int index) {
                        return Container(
                          color: divideColor,
                          height: 10,
                        );
                      },
                      itemCount: dataList.length + ownList.length),
                  onRefresh: () async {
                    page = 1;
                    loadList();
                  },
                  onLoad: () async {
                    page++;
                    loadList();
                  },
                ),
              ),
              Container(
                color: white,
                padding: EdgeInsets.fromLTRB(
                    ScreenUtil.getInstance().setHeight(5),
                    ScreenUtil.getInstance().setHeight(DP_10),
                    ScreenUtil.getInstance().setHeight(DP_20),
                    ScreenUtil.getInstance().setHeight(DP_10)),
                child: Row(
                  children: <Widget>[
                    Checkbox(
                        value: isSelectAll,
                        activeColor: mainColor,
                        checkColor: black_99,
                        materialTapTargetSize: MaterialTapTargetSize.padded,
                        onChanged: (bool x) {
                          isSelectAll = !isSelectAll;
                          checkAll();
                          setState(() {});
                        }),
                    Text('全选',
                        style: TextStyle(
                            fontSize:
                                ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
                            color: Colors.black87)),
                    Container(
                      width: 20,
                    ),
                    Text('合计：',
                        style: TextStyle(
                            fontSize: ScreenUtil.getInstance()
                                .setSp(NORMAL_FONT_SIZE),
                            color: Colors.black)),
                    Text(('$allNum 单'),
                        style: TextStyle(
                            fontSize: ScreenUtil.getInstance()
                                .setSp(NORMAL_FONT_SIZE),
                            color: mainColor)),
                    Expanded(
                      child: Text(''),
                    ),
                    new Container(
                      height: ScreenUtil.getInstance().setHeight(DP_50),
                      decoration: new BoxDecoration(
                          color: mainColor,
                          borderRadius: BorderRadius.circular(10.0)),
                      padding: EdgeInsets.fromLTRB(5.0, 0.0, 5.0, 0.0),
                      child: new FlatButton(
                        child: new Padding(
                          padding: EdgeInsets.all(0.0),
                          child: new Text(
                            "确认下单",
                            style: new TextStyle(
                                color: Colors.white,
                                fontSize: ScreenUtil.getInstance()
                                    .setSp(SMALL_FONT_SIZE)),
                          ),
                        ),
                        onPressed: () {
                          if (allNum == 0) {
                            Fluttertoast.showToast(msg: "请选择订单后再确认提交");
                            return;
                          }
                          String order_id = "";
                          int sumNum = 0;
                          for (int i = 0; i < dataList.length; i++) {
                            if (dataList[i].isSelect) {
                              order_id += dataList[i].order_id + ",";
                              sumNum += int.parse(dataList[i].num);
                            }
                          }
                          for (int i = 0; i < ownList.length; i++) {
                            if (ownList[i].isSelect) {
                              order_id += ownList[i].id + ",";
                              sumNum +=1;
                            }
                          }
                          Navigator.of(context).push<String>(
                              new MaterialPageRoute(builder: (context) {
                            return new ConfirmOrderPage(
                              allPrice:
                                  double.parse(allPrice.toStringAsFixed(2)),
                              allNum: sumNum,
                              order_id:
                                  order_id.substring(0, order_id.length - 1),
                            );
                          })).then((value) {
                            if (value != null) {
                              page = 1;
                              loadList();
                            }
                          });
                        },
                      ),
                    )
                  ],
                ),
              ),
            ],
          )),
    );
  }

  Widget _renderRow(BuildContext context, int index) {
    print(index);
    if (index >= dataList.length) {
      index = index - dataList.length;
      return new GestureDetector(
        onTap: () {
          // Navigator.of(context).push(MaterialPageRoute(builder: (context) {
          //   return new OrderDetailPage(
          //     id: dataList[index].id,
          //   );
          // }));
        },
        child: new Container(
          decoration: BoxDecoration(
              color: Colors.white, borderRadius: BorderRadius.circular(0.0)),
          padding: EdgeInsets.fromLTRB(
              ScreenUtil.getInstance().setHeight(DP_20),
              0,
              ScreenUtil.getInstance().setHeight(DP_20),
              ScreenUtil.getInstance().setHeight(DP_20)),
          child: new Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Row(
                children: <Widget>[
                  Expanded(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Container(height: 10,),
                        Text("下单时间: " + ownList[index].create_time,
                            style: TextStyle(
                                fontSize: ScreenUtil.getInstance()
                                    .setSp(NORMAL_FONT_SIZE-5))),
                        Text("订单号: " + ownList[index].order_num,
                            style: TextStyle(
                                fontSize: ScreenUtil.getInstance()
                                    .setSp(NORMAL_FONT_SIZE-5))),
                      ],
                    ),
                  ),
                  
                  Checkbox(
                      value: ownList[index].isSelect,
                      activeColor: mainColor,
                      checkColor: black_99,
                      materialTapTargetSize: MaterialTapTargetSize.padded,
                      onChanged: (bool x) {
                        ownList[index].isSelect = !ownList[index].isSelect;
                        allNum = 0;
                        allPrice = 0;
                        for (int i = 0; i < ownList.length; i++) {
                          if (ownList[i].isSelect) {
                            allNum++;
                            // for (int n = 0; n < dataList[i].datas.length; n++) {
                            //   allNum += int.parse(dataList[i].datas[n].num);
                            // }
                            allPrice += double.parse(ownList[i].allprice);
                          }
                        }
                        for (int i = 0; i < dataList.length; i++) {
                          if (dataList[i].isSelect) {
                            allNum++;
                            // for (int n = 0; n < dataList[i].datas.length; n++) {
                            //   allNum += int.parse(dataList[i].datas[n].num);
                            // }
                            allPrice += double.parse(dataList[i].allprice);
                          }
                        }
                        setState(() {});
                      }),
                ],
              ),
               Container(height: 7,),
              ListView.builder(
                padding: EdgeInsets.all(0),
                physics: new NeverScrollableScrollPhysics(),
                itemBuilder: (BuildContext context, int index1) {
                  return Container(
                    child: Row(
                      children: <Widget>[
                        ClipRRect(
                          borderRadius: BorderRadius.circular(2),
                          child: CachedNetworkImage(
                imageUrl:
                              Api.ImgBaseUrl + ownList[index].datas[index1].img,
                              width: ScreenUtil.getInstance().setHeight(DP_120),
                              fit: BoxFit.fill,
                              height:
                                  ScreenUtil.getInstance().setHeight(DP_120)),
                        ),
                        Container(
                          width: 10,
                        ),
                         Expanded(
              child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Text(ownList[index].datas[index1].goods_name,
                                maxLines: 2,
                                overflow: TextOverflow.ellipsis,
                                style: TextStyle(
                                    fontSize: ScreenUtil.getInstance()
                                        .setSp(NORMAL_FONT_SIZE))),
                            Container(
                              height: 4,
                            ),
                            Container(
                              width: (MediaQuery.of(context).size.width -
                                  ScreenUtil.getInstance().setHeight(DP_180)),
                              child: Html(
                                  data: "规格: " +
                                      ownList[index].datas[index1].sku_str,
                                  defaultTextStyle: TextStyle(
                                      color: Colors.black45,
                                      fontSize: ScreenUtil.getInstance()
                                          .setSp(SMALL_FONT_SIZE))),
                            ),
                            Container(
                              height: 6,
                            ),
                            Row(
                              children: <Widget>[
                                Text(MONEY + ownList[index].datas[index1].price,
                                    style: TextStyle(
                                        color: mainColor,
                                        fontSize: ScreenUtil.getInstance()
                                            .setSp(SMALL_FONT_SIZE))),
                                Container(
                                  width: 20,
                                ),
                                Text("X" + ownList[index].datas[index1].num,
                                    style: TextStyle(
                                        color: black_99,
                                        fontSize: ScreenUtil.getInstance()
                                            .setSp(SMALL_FONT_SIZE))),
                              ],
                            )
                          ],
                        )
                         )
                      ],
                    ),
                  );
                },
                itemCount: ownList[index].datas.length,
                shrinkWrap: true,
              ),
              Container(
                height: 10,
              ),
              Text(
                  "收货人: " +
                      ownList[index].consignee.toString() +
                      "   " +
                      ownList[index].contact_number.toString(),
                  style: TextStyle(
                      color: black_99,
                      fontSize:
                          ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE))),
              Container(
                width: double.infinity,
                alignment: Alignment.centerRight,
                child: Text("应交金额: " + ownList[index].allprice,
                    style: TextStyle(
                        color: black,
                        fontSize:
                            ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE))),
              ),
              Container(
                height: 10,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.end,
                crossAxisAlignment: CrossAxisAlignment.end,
                children: <Widget>[
                  Offstage(
                    offstage: ownList[index].status != "1",
                    child: GestureDetector(
                      child: Container(
                        padding: EdgeInsets.fromLTRB(10, 2, 10, 2),
                        decoration: BoxDecoration(
                            border: Border.all(color: black_99, width: 1),
                            borderRadius: BorderRadius.circular(30)),
                        child: Text('取消订单',
                            style: TextStyle(
                                fontSize: ScreenUtil.getInstance()
                                    .setSp(SMALL_FONT_SIZE),
                                color: black_99)),
                      ),
                      onTap: () {
                        showDialog(
                          context: context,
                          builder: (context) => new AlertDialog(
                            title: new Text('提示'),
                            content: new Text('确定取消该条订单吗，取消后不可撤回'),
                            actions: <Widget>[
                              new FlatButton(
                                onPressed: () =>
                                    Navigator.of(context).pop(false),
                                child: new Text(
                                  '取消',
                                  style: TextStyle(color: Colors.grey),
                                ),
                              ),
                              new FlatButton(
                                onPressed: () {
                                  Navigator.of(context).pop(false);
                                  cancle(index);
                                },
                                child: new Text('确定取消',
                                    style: TextStyle(color: mainColor)),
                              ),
                            ],
                          ),
                        );
                      },
                    ),
                  ),
                  // Offstage(
                  //   offstage: dataList[index].status != "3",
                  //   child: GestureDetector(
                  //     child: Container(
                  //       margin: EdgeInsets.only(right: 10),
                  //       padding: EdgeInsets.fromLTRB(10, 2, 10, 2),
                  //       decoration: BoxDecoration(
                  //           border: Border.all(color: mainColor, width: 1),
                  //           borderRadius: BorderRadius.circular(30)),
                  //       child: Text('查看物流',
                  //           style: TextStyle(
                  //               fontSize: ScreenUtil.getInstance()
                  //                   .setSp(SMALL_FONT_SIZE),
                  //               color: mainColor)),
                  //     ),
                  //     onTap: () {
                  //       Fluttertoast.showToast(msg: "敬请期待");
                  //     },
                  //   ),
                  // ),
                  // Offstage(
                  //   offstage: dataList[index].status != "3",
                  //   child: GestureDetector(
                  //     child: Container(
                  //       padding: EdgeInsets.fromLTRB(10, 2, 10, 2),
                  //       decoration: BoxDecoration(
                  //           color: mainColor,
                  //           border: Border.all(color: mainColor, width: 1),
                  //           borderRadius: BorderRadius.circular(30)),
                  //       child: Text('确认收货',
                  //           style: TextStyle(
                  //               fontSize: ScreenUtil.getInstance()
                  //                   .setSp(SMALL_FONT_SIZE),
                  //               color: white)),
                  //     ),
                  //     onTap: () {
                  //       showDialog(
                  //         context: context,
                  //         builder: (context) => new AlertDialog(
                  //           title: new Text('提示'),
                  //           content: new Text('确定已经收到货物吗,确认后不可撤回'),
                  //           actions: <Widget>[
                  //             new FlatButton(
                  //               onPressed: () =>
                  //                   Navigator.of(context).pop(false),
                  //               child: new Text(
                  //                 '取消',
                  //                 style: TextStyle(color: Colors.grey),
                  //               ),
                  //             ),
                  //             new FlatButton(
                  //               onPressed: () {
                  //                 Navigator.of(context).pop(false);
                  //                 confirm(index);
                  //               },
                  //               child: new Text('确定',
                  //                   style: TextStyle(color: mainColor)),
                  //             ),
                  //           ],
                  //         ),
                  //       );
                  //     },
                  //   ),
                  // )
                ],
              )
            ],
          ),
        ),
      );
    }
    return new GestureDetector(
      onTap: () {
        // Navigator.of(context).push(MaterialPageRoute(builder: (context) {
        //   return new OrderDetailPage(
        //     id: dataList[index].id,
        //   );
        // }));
      },
      child: new Container(
        decoration: BoxDecoration(
            color: Colors.white, borderRadius: BorderRadius.circular(0.0)),
        padding: EdgeInsets.all(ScreenUtil.getInstance().setHeight(DP_20)),
        child: new Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Row(
              children: <Widget>[
                ClipOval(
                  child: dataList[index].head_img == null
                      ? Image.asset("images/app_icon.png",
                          width: ScreenUtil.getInstance().setHeight(DP_60),
                          height: ScreenUtil.getInstance().setHeight(DP_60),
                          fit: BoxFit.cover)
                      : CachedNetworkImage(
                imageUrl:
                          dataList[index].head_img.contains("http")
                              ? dataList[index].head_img
                              : Api.ImgBaseUrl + dataList[index].head_img,
                          width: ScreenUtil.getInstance().setHeight(DP_60),
                          height: ScreenUtil.getInstance().setHeight(DP_60),
                          fit: BoxFit.cover),
                ),
                Container(width: 10),
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Row(
                      children: <Widget>[
                        new Text(
                          dataList[index].name == null
                              ? ""
                              : dataList[index].name,
                          style: new TextStyle(
                              color: black,
                              fontSize: ScreenUtil.getInstance()
                                  .setSp(NORMAL_FONT_SIZE)),
                        ),
                        Container(width: 5),
                        Container(
                          decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(2),
                              border: Border.all(color: mainColor, width: 1)),
                          padding: EdgeInsets.fromLTRB(
                              ScreenUtil.getInstance().setHeight(DP_8),
                              0,
                              ScreenUtil.getInstance().setHeight(DP_8),
                              0),
                          margin: EdgeInsets.fromLTRB(0, 0,
                              ScreenUtil.getInstance().setHeight(DP_10), 0),
                          child: Text(
                            dataList[index].group_name,
                            style: TextStyle(
                                fontSize: ScreenUtil.getInstance()
                                    .setSp(MINI_FONT_SIZE),
                                color: mainColor),
                          ),
                        )
                      ],
                    ),
                    Container(height: 3),
                    new Text(
                      dataList[index].create_time,
                      style: new TextStyle(
                          color: Colors.black45,
                          fontSize:
                              ScreenUtil.getInstance().setSp(MINI_FONT_SIZE)),
                    ),
                  ],
                ),
                Expanded(
                  child: Center(),
                ),
                Checkbox(
                    value: dataList[index].isSelect,
                    activeColor: mainColor,
                    checkColor: black_99,
                    materialTapTargetSize: MaterialTapTargetSize.padded,
                    onChanged: (bool x) {
                      dataList[index].isSelect = !dataList[index].isSelect;
                      allNum = 0;
                      allPrice = 0;
                      for (int i = 0; i < dataList.length; i++) {
                        if (dataList[i].isSelect) {
                          allNum++;
                          // for (int n = 0; n < dataList[i].datas.length; n++) {
                          //   allNum += int.parse(dataList[i].datas[n].num);
                          // }
                          allPrice += double.parse(dataList[i].allprice);
                        }
                      }
                      for (int i = 0; i < ownList.length; i++) {
                        if (ownList[i].isSelect) {
                          allNum++;
                          // for (int n = 0; n < dataList[i].datas.length; n++) {
                          //   allNum += int.parse(dataList[i].datas[n].num);
                          // }
                          allPrice += double.parse(ownList[i].allprice);
                        }
                      }
                      setState(() {});
                    }),
                // Offstage(
                //   offstage: widget.type != 1,
                //   child: CircularCheckBox(
                //       value: dataList[index].isSelect,
                //       activeColor: mainColor,
                //       inactiveColor: black_99,
                //       materialTapTargetSize: MaterialTapTargetSize.padded,
                //       onChanged: (bool x) {
                //         dataList[index].isSelect = !dataList[index].isSelect;
                //         allNum = 0;
                //         allPrice = 0;
                //         for (int i = 0; i < dataList.length; i++) {
                //           if (dataList[index].isSelect)
                //             allNum += int.parse(dataList[i].num);
                //           allPrice += double.parse(dataList[i].allprice);
                //         }
                //         setState(() {});
                //       }),
                // )
              ],
            ),
            Container(height: 10),
            new GestureDetector(
              child: Container(
                padding: EdgeInsets.fromLTRB(30, 20, 30, 20),
                decoration: BoxDecoration(
                  color: f0Color,
                  borderRadius: BorderRadius.circular(5),
                ),
                child: Row(
                  children: <Widget>[
                    new Text(
                      "总额:   ",
                      style: new TextStyle(
                          color: Colors.black54,
                          fontSize:
                              ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)),
                    ),
                    new Text(
                      MONEY + dataList[index].allprice,
                      style: new TextStyle(
                          color: mainColor,
                          fontSize:
                              ScreenUtil.getInstance().setSp(BIG_FONT_SIZE)),
                    ),
                    Container(width: 30),
                    new Text(
                      "单数:   ",
                      style: new TextStyle(
                          color: Colors.black54,
                          fontSize:
                              ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)),
                    ),
                    new Text(
                      dataList[index].num,
                      style: new TextStyle(
                          color: mainColor,
                          fontSize:
                              ScreenUtil.getInstance().setSp(BIG_FONT_SIZE)),
                    ),
                    new Text(
                      "个",
                      style: new TextStyle(
                          color: Colors.black54,
                          fontSize:
                              ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)),
                    ),
                  ],
                ),
              ),
              onTap: () {
                Navigator.of(context)
                    .push(new MaterialPageRoute(builder: (context) {
                  return new ChildOrderListPage(
                      id: dataList[index].parameter, type: "4");
                }));
              },
              behavior: HitTestBehavior.opaque,
            ),
            Container(
              height: 10,
            ),
            Row(
              children: <Widget>[
                Expanded(
                    child: Offstage(
                  offstage: widget.type != 3,
                  child: new Text(
                    "积分兑换: 600",
                    style: new TextStyle(
                        color: Colors.black87,
                        fontSize: ScreenUtil.getInstance()
                            .setSp(SMALL_FONT_SIZE + 2)),
                  ),
                )),
                new Text(
                  "应交总额: " + dataList[index].allprice,
                  style: new TextStyle(
                      color: Colors.black87,
                      fontSize:
                          ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE + 2)),
                ),
              ],
            ),
            Container(
              height: 10,
            ),
            Offstage(
                offstage: widget.type != 3,
                child: new Text(
                  "备注:",
                  style: new TextStyle(
                      color: Colors.black87,
                      fontSize:
                          ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE + 2)),
                ))
          ],
        ),
      ),
    );
  }

  del(id) async {
    FormData formData = new FormData.fromMap({
      "token": token,
      "id": id,
    });
    Response<String> response =
        await HttpUtil().post(Api.DEL_ORDER, data: formData);
    Map userMap = json.decode(response.data);
    int code = userMap["code"];
    String msg = userMap["msg"];
    Navigator.of(context).pop(1);
    Fluttertoast.showToast(msg: msg);
    if (code == 0) {
      page = 1;
      loadList();
    }
    return true;
  }

  checkAll() {
    allNum = 0;
    allPrice = 0;
    for (int i = 0; i < dataList.length; i++) {
      dataList[i].isSelect = isSelectAll;
      if (isSelectAll) {
        // for (int n = 0; n < dataList[i].datas.length; n++) {
        allNum++;
        // }
        allPrice += double.parse(dataList[i].allprice);
      }
    }
    for (int i = 0; i < ownList.length; i++) {
      ownList[i].isSelect = isSelectAll;
      if (isSelectAll) {
        // for (int n = 0; n < dataList[i].datas.length; n++) {
        allNum++;
        // }
        allPrice += double.parse(ownList[i].allprice);
      }
    }
    setState(() {});
  }

  confirm(int index) async {
    Map map = new Map();
    map['token'] = token;
    map['order_id'] = dataList[index].id;
    map['sign_type'] = SignType.CONFIRM_ORDER;
    Response<String> response =
        await HttpUtil().post(Api.CONFIRM_ORDER, data: getSign(map));
    Map userMap = json.decode(response.data);
    int code = userMap["code"];
    String msg = userMap["msg"];
    Fluttertoast.showToast(msg: msg);
    if (code == 1 && mounted) {
      APPLIC.eventBus.fire(OrderRefreshEvent("2"));
      dataList.removeAt(index);
      setState(() {});
    }
    return true;
  }

  cancle(int index) async {
    Map map = new Map();
    map['token'] = token;
    map['order_id'] = ownList[index].id;
    map['sign_type'] = SignType.CANCLE_ORDER;
    Response<String> response =
        await HttpUtil().post(Api.CANCLE_ORDER, data: getSign(map));
    Map userMap = json.decode(response.data);
    int code = userMap["code"];
    String msg = userMap["msg"];
    Fluttertoast.showToast(msg: msg);
    if (code == 1 && mounted) {
      ownList.removeAt(index);
      setState(() {});
    }
    return true;
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
